//
//  SSFirstPageBookingHotelView.m
//  Travel_World
//
//  Created by dlios on 15/10/14.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "SSFirstPageBookingHotelView.h"
#import "QTAFNnetWorkTool.h"
#import "UIImageView+WebCache.h"
#import "SSFirstPageTravelCityModel.h"
#import "SSFirstPageTravelHotCityCellCell.h"
#import "SSFirstPageTravelView.h"
#import "MRCityViewController.h"
#import "Masonry.h"
#import "SSFirstPageWebView.h"

@interface SSFirstPageBookingHotelView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UICollectionView *hotCityCollectionView;
@property (nonatomic, strong) UIButton *tripButton;
@property (nonatomic, strong) NSMutableArray *hotCityArr;
@property (nonatomic, copy) NSString *hotCityURL;
@property (nonatomic, strong) SSFirstPageTravelCityModel *cityModel;

@end

@implementation SSFirstPageBookingHotelView


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hotCityURL = @"http://open.qyer.com/qyer/hotel/hot_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=9&lat=38.8826458947218&lon=121.5397497056552&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&page=1&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%25208.1.2&track_user_id=6654242&v=1%20";
    }
    
    return self;
}


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.layer.masksToBounds = NO;
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.navigationController.navigationBar.translucent = NO;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"订酒店";
    [self createMainScrollView];
    [self createBackgroundImageView];
    [self createHotCityCollectionView];
    [self createData];
}


- (void)createMainScrollView
{
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.mainScrollView.showsVerticalScrollIndicator = NO;
    self.mainScrollView.bounces = NO;
    self.mainScrollView.contentSize = CGSizeMake(WIDTH, 800);
    [self.view addSubview:self.mainScrollView];
    
    
    
    [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.top.bottom.equalTo(self.view);
   
    }];
    
}

- (void)createBackgroundImageView
{
    self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0.6 *  WIDTH)];
    self.backgroundImageView.image = [UIImage imageNamed:@"A516Y1D5[G28ZD5PLON[7I1.jpg"];
    [self.mainScrollView addSubview:self.backgroundImageView];
    
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.mainScrollView.mas_top).offset(10);
        make.left.equalTo(self.mainScrollView.mas_left).offset(10);
        make.right.equalTo(self.mainScrollView.mas_right).offset(10);
        make.width.equalTo(self.mainScrollView.mas_width).offset(- 20);
        make.height.equalTo(self.backgroundImageView.mas_width).multipliedBy(0.6);
        
    }];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, WIDTH * 0.1, WIDTH, 30)];
    titleLabel.text = @"全球超过540,000家酒店在线预定";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor redColor];
    [self.mainScrollView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backgroundImageView.mas_bottom).multipliedBy(0.2);
        make.left.and.right.equalTo(self.backgroundImageView);
        make.width.equalTo(self.backgroundImageView.mas_width);
        
    }];
    
    
    self.tripButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.tripButton.backgroundColor = [UIColor blackColor];
    self.tripButton.alpha = 0.6;
    [self.tripButton setTitle:@"选择目的地" forState:UIControlStateNormal];
    [self.tripButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.tripButton addTarget:self action:@selector(tripButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainScrollView addSubview:self.tripButton];
    
    [self.tripButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backgroundImageView.mas_bottom).multipliedBy(0.55);
        make.left.equalTo(self.backgroundImageView.mas_right).multipliedBy(0.2);
        make.width.equalTo(self.backgroundImageView.mas_width).multipliedBy(0.6);
        make.height.equalTo(@(30));
        
    }];
    
    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    selectButton.frame = CGRectMake(WIDTH * 0.7, WIDTH * 0.5, WIDTH * 0.2, 30);
    selectButton.backgroundColor = [UIColor brownColor];
    [selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [selectButton setTitle:@"选择酒店" forState:UIControlStateNormal];
    [selectButton addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainScrollView addSubview:selectButton];
    
    [selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backgroundImageView.mas_bottom).multipliedBy(0.8);
        make.left.equalTo(self.backgroundImageView.mas_right).multipliedBy(0.7);
        make.width.equalTo(self.backgroundImageView.mas_width).multipliedBy(0.25);
        make.bottom.equalTo(self.backgroundImageView.mas_bottom).multipliedBy(0.95);
        
    }];

    
    
    UILabel *hotLabel = [[UILabel alloc] init];
    [self.mainScrollView addSubview:hotLabel];
    hotLabel.text = @"热门酒店";
    hotLabel.font = [UIFont systemFontOfSize:24];
    hotLabel.textAlignment = NSTextAlignmentCenter;
    hotLabel.textColor = [UIColor redColor];
    
    [hotLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backgroundImageView.mas_bottom).offset(30);
        make.left.equalTo(self.mainScrollView.mas_left);
        make.right.equalTo(self.mainScrollView.mas_right);
        make.width.equalTo(self.mainScrollView);
        make.height.equalTo(@50);
    }];
    
}


- (void)tripButtonClick:(UIButton *)button
{
    void(^block)(SSFirstPageTravelCityModel *) = ^(SSFirstPageTravelCityModel *citymodel){
        
        [self.tripButton setTitle:citymodel.cnname forState:UIControlStateNormal];
        self.cityModel = citymodel;
        
    };
    
    SSFirstPageTravelView *firstPageTravelView = [[SSFirstPageTravelView alloc] init];
    firstPageTravelView.ifReturnLastInterface = YES;
    firstPageTravelView.block = block;
    UINavigationController *firstPageTravelNC = [[UINavigationController alloc] initWithRootViewController:firstPageTravelView];
    
    [self presentViewController:firstPageTravelNC animated:YES completion:nil];
}

- (void)selectButtonClick:(UIButton *)button
{
    if (![self.tripButton.titleLabel.text isEqualToString:@"选择目的地"]) {
        NSLog(@"跳转");
        
        NSString *url = [NSString stringWithFormat:@"http://appview.qyer.com/index.php?action=hotelDetail&spm=index&aid=352437&cityid=%@", self.cityModel.myid];
        
        
        NSLog(@"%@", url);
        SSFirstPageWebView *cityViewController = [[SSFirstPageWebView alloc] init];
        cityViewController.myURL = url;
        [self.navigationController pushViewController:cityViewController animated:YES];

        
    } else {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择目的地" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
}


- (void)createHotCityCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(MINW / 4, MINW / 4);
    flowLayout.minimumInteritemSpacing = MINW / 16;
    flowLayout.minimumLineSpacing = MINW / 16;
    flowLayout.sectionInset = UIEdgeInsetsMake(MINW / 16, MINW / 16, MINW / 16, MINW / 16);
    
    self.hotCityCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, MINW, MINW, MINW) collectionViewLayout:flowLayout];
    self.hotCityCollectionView.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.hotCityCollectionView];
    self.hotCityCollectionView.delegate = self;
    self.hotCityCollectionView.dataSource = self;

    [self.hotCityCollectionView registerClass:[SSFirstPageTravelHotCityCellCell class] forCellWithReuseIdentifier:@"city"];
    
    [self.hotCityCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.backgroundImageView.mas_bottom).offset(100);
        make.left.equalTo(self.mainScrollView.mas_left);
        make.right.equalTo(self.mainScrollView.mas_right);
        make.width.equalTo(self.mainScrollView);
        make.height.equalTo(@(MINW));
        
    }];
    
    
    [self.mainScrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.hotCityCollectionView.mas_bottom);
    }];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.hotCityArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SSFirstPageTravelHotCityCellCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"city" forIndexPath:indexPath];
    [cell.cityImageView sd_setImageWithURL:[NSURL URLWithString:[self.hotCityArr[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"placeholder1"]];
    
    cell.cnnameLabel.text = [self.hotCityArr[indexPath.row] cnname];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SSFirstPageTravelCityModel *cityModel = self.hotCityArr[indexPath.row];
    
    NSString *url = [NSString stringWithFormat:@"http://appview.qyer.com/index.php?action=hotelDetail&spm=index&aid=352437&cityid=%@", cityModel.myid];
    
    SSFirstPageWebView *cityViewController = [[SSFirstPageWebView alloc] init];
    cityViewController.myURL = url;
    [self.navigationController pushViewController:cityViewController animated:YES];
}

- (void)createData
{
    [QTAFNnetWorkTool getUrl:self.hotCityURL body:nil hudSuperView:self.view result:QTJSON headerFile:nil success:^(id result) {
        
        self.hotCityArr = [SSFirstPageTravelCityModel baseModelByArr:result[@"data"]];
        [self.hotCityCollectionView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
